package com.abc.recycling.controller;

import com.abc.recycling.domain.MessageTemplate;
import com.abc.recycling.service.MessageTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/template")
@RequiredArgsConstructor
public class MessageTemplateController {

    private final MessageTemplateService templateService;

    /**
     * 模板列表页面
     */
    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN')")
    public String list(@RequestParam(required = false) String type,
                      @RequestParam(required = false) Integer status,
                      @RequestParam(required = false) String keyword,
                      Model model) {
        List<MessageTemplate> templates = templateService.searchTemplates(type, status, keyword);
        model.addAttribute("templates", templates);
        model.addAttribute("type", type);
        model.addAttribute("status", status);
        model.addAttribute("keyword", keyword);
        return "template/list";
    }

    /**
     * 创建模板页面
     */
    @GetMapping("/create")
    @PreAuthorize("hasRole('ADMIN')")
    public String createForm() {
        return "template/edit";
    }

    /**
     * 编辑模板页面
     */
    @GetMapping("/edit/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public String editForm(@PathVariable Long id, Model model) {
        MessageTemplate template = templateService.getTemplate(id);
        model.addAttribute("template", template);
        return "template/edit";
    }

    /**
     * 创建模板
     */
    @PostMapping("/create")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public ResponseEntity<?> create(@RequestBody MessageTemplate template) {
        try {
            templateService.createTemplate(template);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 更新模板
     */
    @PutMapping("/update")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public ResponseEntity<?> update(@RequestBody MessageTemplate template) {
        try {
            templateService.updateTemplate(template);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 删除模板
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public ResponseEntity<?> delete(@PathVariable Long id) {
        try {
            templateService.deleteTemplate(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 更新模板状态
     */
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public ResponseEntity<?> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            templateService.updateTemplateStatus(id, status);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 